﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class CoilConnectForm : System.Windows.Forms.Form
	{
		public CoilConnectForm(Document doc)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			//base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			this.initParameterSetting();
		}

		private void btnSelectSupply_Click(object sender, EventArgs e)
		{
			this.supplyAccessory = this.GetAccessory(this.supplyAccessory);
			if (this.supplyAccessory != null)
			{
				this.lbSupply.Items.Clear();
				foreach (FamilySymbol familySymbol in this.supplyAccessory)
				{
					if (familySymbol.Family.Name.Length > 3 && familySymbol.Family.Name.Substring(0, 3).Equals("HY-"))
					{
						this.lbSupply.Items.Add(familySymbol.Family.Name.Substring(3));
					}
					else
					{
						this.lbSupply.Items.Add(familySymbol.Family.Name);
					}
				}
			}
		}

		private void btnSelectReturn_Click(object sender, EventArgs e)
		{
			this.returnAccessory = this.GetAccessory(this.returnAccessory);
			if (this.returnAccessory != null)
			{
				this.lbReturn.Items.Clear();
				foreach (FamilySymbol familySymbol in this.returnAccessory)
				{
					if (familySymbol.Family.Name.Length > 3 && familySymbol.Family.Name.Substring(0, 3).Equals("HY-"))
					{
						this.lbReturn.Items.Add(familySymbol.Family.Name.Substring(3));
					}
					else
					{
						this.lbReturn.Items.Add(familySymbol.Family.Name);
					}
				}
			}
		}

		public bool BeFlex()
		{
			return this.cbFlex.Checked;
		}

		private List<FamilySymbol> GetAccessory(List<FamilySymbol> initialSymbols)
		{
			FrmGroupValue frmGroupValue = new FrmGroupValue(this.doc, 2);
			frmGroupValue.IsShowLayType(false);
			DialogResult dialogResult = frmGroupValue.ShowDialog();
			if (DialogResult.OK == dialogResult)
			{
				return frmGroupValue.LayFamilySymbol;
			}
			return initialSymbols;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			try
			{
				if (double.Parse(this.hytbSupplyLength.Text) > 10000.0 || double.Parse(this.hytbSupplyLength.Text) < 10.0)
				{
					YJKMessageBox.Information("供水管长度设置过长或过短，长度单位为毫米，请查看。");
					this.hytbSupplyLength.Focus();
					return;
				}
				if (double.Parse(this.hytbReturnLength.Text) > 10000.0 || double.Parse(this.hytbReturnLength.Text) < 10.0)
				{
					YJKMessageBox.Information("回水管长度设置过长或过短，长度单位为毫米，请查看。");
					this.hytbReturnLength.Focus();
					return;
				}
				if (double.Parse(this.hytbColdLength.Text) > 10000.0 || double.Parse(this.hytbColdLength.Text) < 10.0)
				{
					YJKMessageBox.Information("冷凝水管长度设置过长或过短，长度单位为毫米，请查看。");
					this.hytbColdLength.Focus();
					return;
				}
				if (this.cbFlex.Checked)
				{
					if (double.Parse(this.hytbFlexLength.Text) > 10000.0 || double.Parse(this.hytbFlexLength.Text) < 100.0)
					{
						YJKMessageBox.Information("软管水管长度设置过长或过短，长度单位为毫米，请查看。");
						this.hytbFlexLength.Focus();
						return;
					}
					this.bFlex = true;
					this.FlexLength = double.Parse(this.hytbFlexLength.Text);
				}
				else
				{
					this.bFlex = false;
				}
			}
			catch (Exception)
			{
			}
			this.maxDistance = this.maxDis();
			this.FlexLength = double.Parse(this.hytbFlexLength.Text);
			this.accessory = new List<FamilySymbol>(4)
			{
				null,
				null,
				null,
				null
			};
			if (this.supplyAccessory != null && this.supplyAccessory.Count > 0)
			{
				this.connectionType = 1;
				int num = 0;
				foreach (FamilySymbol value in this.supplyAccessory)
				{
					this.accessory[num] = value;
					num++;
				}
			}
			if (this.returnAccessory != null && this.returnAccessory.Count > 0)
			{
				this.connectionType = 2;
				int num2 = 2;
				foreach (FamilySymbol value2 in this.returnAccessory)
				{
					this.accessory[num2] = value2;
					num2++;
				}
			}
			this.UserSettingWrite();
			base.DialogResult = DialogResult.OK;
		}

		private int maxDis()
		{
			int num = 600;
			try
			{
				int num2 = int.Parse(this.hytbSupplyLength.Text);
				int num3 = int.Parse(this.hytbReturnLength.Text);
				int num4 = int.Parse(this.hytbColdLength.Text);
				if (num2 < num3)
				{
					num = num3;
				}
				else
				{
					num = num2;
				}
				if (num < num4)
				{
					num = num4;
				}
			}
			catch (Exception)
			{
			}
			return num;
		}

		private void UserSettingWrite()
		{
			try
			{
				string text = "";
				foreach (object obj in this.lbSupply.Items)
				{
					text = text + obj.ToString() + ",";
				}
				text += "|";
				foreach (object obj2 in this.lbReturn.Items)
				{
					text = text + obj2.ToString() + ",";
				}
				EquipmentCoilSetParam equipmentCoilSetParam = new EquipmentCoilSetParam();
				equipmentCoilSetParam.SupplyWater = double.Parse(this.hytbSupplyLength.Text.Trim());
				equipmentCoilSetParam.ReturnWater = double.Parse(this.hytbReturnLength.Text.Trim());
				equipmentCoilSetParam.CoolWater = double.Parse(this.hytbColdLength.Text.Trim());
				equipmentCoilSetParam.DefaultValve = text;
				this.userSetting.WriteEquipmentCoilSetText(equipmentCoilSetParam);
				this.userSetting.WriteValue("equipmentcoilpipeset", "cbflex", this.cbFlex.Checked.ToString());
				this.userSetting.WriteValue("equipmentcoilpipeset", "hytbFlexLength", this.hytbFlexLength.Text.ToString());
			}
			catch (Exception)
			{
			}
		}

		private void initParameterSetting()
		{
			EquipmentCoilSetParam equipmentCoilSetText = this.userSetting.GetEquipmentCoilSetText();
			try
			{
				this.hytbSupplyLength.Text = equipmentCoilSetText.SupplyWater.ToString();
				this.hytbReturnLength.Text = equipmentCoilSetText.ReturnWater.ToString();
				this.hytbColdLength.Text = equipmentCoilSetText.CoolWater.ToString();
				string defaultValve = equipmentCoilSetText.DefaultValve;
				this.cbFlex.Checked = Convert.ToBoolean((this.userSetting.ReadValue("equipmentcoilpipeset", "cbflex") == "") ? "True" : this.userSetting.ReadValue("equipmentcoilpipeset", "cbflex"));
				this.hytbFlexLength.Text = ((this.userSetting.ReadValue("equipmentcoilpipeset", "hytbFlexLength") == "") ? "150" : this.userSetting.ReadValue("equipmentcoilpipeset", "hytbFlexLength"));
				string[] array = defaultValve.Split(new char[]
				{
					'|'
				});
				if (array.Length > 1)
				{
					if (!string.IsNullOrEmpty(array[0]))
					{
						foreach (string item in array[0].Substring(0, array[0].Length - 1).Split(new char[]
						{
							','
						}))
						{
							this.lbSupply.Items.Add(item);
						}
					}
					if (!string.IsNullOrEmpty(array[1]))
					{
						foreach (string item2 in array[1].Substring(0, array[1].Length - 1).Split(new char[]
						{
							','
						}))
						{
							this.lbReturn.Items.Add(item2);
						}
					}
				}
				SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
				foreach (object obj in this.lbSupply.Items)
				{
					if (!string.IsNullOrEmpty(obj.ToString()))
					{
						FamilySymbol familySymbolByNameSubTran = spaceFamilyDataReader.GetFamilySymbolByNameSubTran(obj.ToString(), "PipeValve", this.doc, BuiltInCategory.OST_PipeAccessory);
						if (familySymbolByNameSubTran != null)
						{
							if (this.supplyAccessory == null)
							{
								this.supplyAccessory = new List<FamilySymbol>();
							}
							this.supplyAccessory.Add(familySymbolByNameSubTran);
						}
					}
				}
				foreach (object obj2 in this.lbReturn.Items)
				{
					if (!string.IsNullOrEmpty(obj2.ToString()))
					{
						FamilySymbol familySymbolByNameSubTran2 = spaceFamilyDataReader.GetFamilySymbolByNameSubTran(obj2.ToString(), "PipeValve", this.doc, BuiltInCategory.OST_PipeAccessory);
						if (familySymbolByNameSubTran2 != null)
						{
							if (this.returnAccessory == null)
							{
								this.returnAccessory = new List<FamilySymbol>();
							}
							this.returnAccessory.Add(familySymbolByNameSubTran2);
						}
					}
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message + "操作记录读取失败，请重新设置");
				this.lbSupply.Items.Clear();
				this.lbReturn.Items.Clear();
			}
		}

		private void lbSupply_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.lbSupply.SelectedItem != null)
			{
				foreach (FamilySymbol familySymbol in this.supplyAccessory)
				{
					if (familySymbol.Family.Name.Equals((this.lbSupply.SelectedItem as string) ?? ""))
					{
						this.supplyAccessory.Remove(familySymbol);
						break;
					}
				}
				this.lbSupply.Items.Remove(this.lbSupply.SelectedItem);
			}
		}

		private void lbReturn_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void lbReturn_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.lbReturn.SelectedItem != null)
			{
				foreach (FamilySymbol familySymbol in this.returnAccessory)
				{
					if (familySymbol.Family.Name.Equals((this.lbReturn.SelectedItem as string) ?? ""))
					{
						this.returnAccessory.Remove(familySymbol);
						break;
					}
				}
				this.lbReturn.Items.Remove(this.lbReturn.SelectedItem);
			}
		}

		private void btnTrack_Click(object sender, EventArgs e)
		{
			try
			{
				if (double.Parse(this.hytbSupplyLength.Text) > 10000.0 || double.Parse(this.hytbSupplyLength.Text) < 10.0)
				{
					YJKMessageBox.Information("供水管长度设置过长或过短，长度单位为毫米，请查看。");
					this.hytbSupplyLength.Focus();
					return;
				}
				if (double.Parse(this.hytbReturnLength.Text) > 10000.0 || double.Parse(this.hytbReturnLength.Text) < 10.0)
				{
					YJKMessageBox.Information("回水管长度设置过长或过短，长度单位为毫米，请查看。");
					this.hytbReturnLength.Focus();
					return;
				}
				if (double.Parse(this.hytbColdLength.Text) > 10000.0 || double.Parse(this.hytbColdLength.Text) < 10.0)
				{
					YJKMessageBox.Information("冷凝水管长度设置过长或过短，长度单位为毫米，请查看。");
					this.hytbColdLength.Focus();
					return;
				}
				if (this.cbFlex.Checked)
				{
					if (double.Parse(this.hytbFlexLength.Text) > 10000.0 || double.Parse(this.hytbFlexLength.Text) < 10.0)
					{
						YJKMessageBox.Information("软管水管长度设置过长或过短，长度单位为毫米，请查看。");
						this.hytbFlexLength.Focus();
						return;
					}
					this.bFlex = true;
					this.FlexLength = double.Parse(this.hytbFlexLength.Text);
				}
			}
			catch (Exception)
			{
			}
			this.maxDistance = this.maxDis();
			this.FlexLength = double.Parse(this.hytbFlexLength.Text);
			this.accessory = new List<FamilySymbol>(4)
			{
				null,
				null,
				null,
				null
			};
			if (this.supplyAccessory != null && this.supplyAccessory.Count > 0)
			{
				this.connectionType = 1;
				int num = 0;
				foreach (FamilySymbol value in this.supplyAccessory)
				{
					this.accessory[num] = value;
					num++;
				}
			}
			if (this.returnAccessory != null && this.returnAccessory.Count > 0)
			{
				this.connectionType = 2;
				int num2 = 2;
				foreach (FamilySymbol value2 in this.returnAccessory)
				{
					this.accessory[num2] = value2;
					num2++;
				}
			}
			this.UserSettingWrite();
			base.DialogResult = DialogResult.Yes;
		}

		private void cbFlex_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbFlex.Checked)
			{
				this.hytbFlexLength.Enabled = true;
				return;
			}
			this.hytbFlexLength.Enabled = false;
		}

		private Document doc;

		private List<FamilySymbol> supplyAccessory;

		private List<FamilySymbol> returnAccessory;

		public double FlexLength;

		public List<FamilySymbol> accessory;

		public int connectionType;

		public int maxDistance = 600;

		public bool bFlex;

		private HVACUserSetting userSetting = new HVACUserSetting();
	}
}
